﻿using LPY.LeHealth.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPY.LeHealth.DAL
{
    using ServiceStack.OrmLite;
    public class HospitalDAL : GenericBaseDAL<HospitalDTO>
    {
        
        /// <summary>
        /// 获取医院列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public List<HospitalDTO> GetList(int pageIndex,int pageSize,out long totalSize)
        {
            using (var cnn = DbConnection)
            {
                var sql = cnn.From<HospitalDTO>()
                    .LeftJoin<AreaDTO>()
                    .LeftJoin<GradeDTO>()
                    .OrderByDescending(m => m.CreateTime)
                    .Skip((pageIndex - 1) * pageSize)
                    .Take(pageSize);

                //var stat= sql.ToSelectStatement();
                
                totalSize = cnn.Count<HospitalDTO>();

                var rtv=new List<HospitalDTO>();

                try
                {
                    //rtv = cnn.LoadSelect<HospitalDTO>(sql);//该方法有BUG

                    rtv = cnn.Select<HospitalDTO>(sql);
                    var grades = cnn.Select<GradeDTO>();
                    var areas = cnn.Select<AreaDTO>();
                    rtv.Merge(grades);
                    rtv.Merge(areas);
                }
                catch (Exception)
                {
                    //Console.WriteLine(cnn.GetLastSql());
                    throw;
                }

                return rtv;
            }
        }

        /// <summary>
        /// 根据医院Id获取医院信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public HospitalDTO GetById(int id)
        {
            using (var cnn=DbConnection)
            {
                //查询表达式
                var sql = cnn.From<HospitalDTO>()
                    .Where(m => m.Id == id);

                //查询
                var rtv = cnn.LoadSelect(sql).FirstOrDefault();
                return rtv;
            }
        }

        /// <summary>
        /// 添加医院
        /// </summary>
        /// <param name="hos"></param>
        /// <returns></returns>
        public bool Add(HospitalDTO hos)
        {
            using (var cnn=DbConnection)
            {
                var rtv= cnn.Insert<HospitalDTO>(hos);
                return rtv > 0;
            }
        }

        public bool Edit(HospitalDTO hos)
        {
            using (var cnn = DbConnection)
            {
                var data = cnn.SingleById<HospitalDTO>(hos.Id);
                data.Name = hos.Name;
                data.ExamTime =hos.ExamTime;
                data.Detail = hos.Detail;
                data.AppointmentDay = hos.AppointmentDay;
                data.Address = hos.Address;
                data.GradeId = hos.GradeId;
                data.AreaId = hos.AreaId;
                var result = cnn.Update<HospitalDTO>(data);

                return result > 0;
            }

        }

        //（根据Id）删除
        public bool DeletePhysicalItemById(int id)
        {
            using (var cnn = DbConnection)
            {
                var result = cnn.DeleteById<HospitalDTO>(id);
                return result > 0;
            }
        }
        public List<HospitalDTO> GetAll()
        {
            using (var cnn=DbConnection)
            {
                var rtv = cnn.Select<HospitalDTO>();
                return rtv;
            }
        }
    }
}
